using("GridColumn");
DataGrid.inherit("Control");

//Row classes: ["imsgridheader","imsgridfooter","innertablecellselected","innertablecell","innertablecellalt"];
function DataGrid(gridId, lineSelectionId, isHeaderVisible, isFooterVisible){
	this.lineSelection=document.getElementById(lineSelectionId);
	this.selectedClassName="innertablecellselected";
	this.table=null;
	this.isHeaderVisible = isHeaderVisible;
	this.isFooterVisible = isFooterVisible;
    this.columns = new Array();
	
	var grid=document.getElementById(gridId);
	if (grid==null) return;
	if (grid.tagName=="TABLE") {
		this.table=grid;
	} else {
		this.table=grid.getElementsByTagName("TABLE")[0];
	}
	
	Framework.instance.registerFinalizer(this);	
}

DataGrid.prototype.getColumn = function (index) {
    if (this.table==null) return null;
    var column = this.columns[index];
    if (Utils.isDefined(column)) return column;
    column = new GridColumn(this, index);
    this.columns[index] = column;
    return column;   
}

DataGrid.prototype.getColCount = function () {
    if (this.table==null) return 0;
    var colCount=0;
    var row = this.table.rows[0];
    for (var i=0; i<row.cells.length; i++){
        var cell = row.cells[i];
        colCount+=cell.colSpan?cell.colSpan:1;    
    }
    return colCount;
}

DataGrid.prototype.getRowCount = function () {
    if (this.table==null||this.table.rows==null) return 0;
    return this.table.rows.length;
}

DataGrid.prototype.getDataRowCount = function () {
    if (this.table==null||this.table.rows==null) return 0;
    var count = this.table.rows.length-1;
    if (this.isHeaderVisible) count--;
    if (this.isFooterVisible) count--;
    return count;
}

DataGrid.prototype.deselectAll=function () {
    if (!Utils.isDefined(this.table)) return;
	for(var i=0;i<this.table.rows.length;i++){
		var row=this.table.rows[i];
		if (row.className!="innertablecellselected") continue;
		row.className=row.getAttribute('imsgridstyle');
	}
	this.lineSelection.value="";
}

DataGrid.prototype.selectLine=function (sourceElement, params, deselectOthers) {
	if (!Utils.isDefined(deselectOthers)||deselectOthers){
		this.deselectAll();
	}
	//TODO: add support for multiple selection
	this.lineSelection.value=params;
	var row=this.findElementRow(sourceElement);
	if (row!=null){
		row.className=this.selectedClassName;
	}
	return false;
}

DataGrid.prototype.deselectLine=function (sourceElement) {
	var row=this.findElementRow(sourceElement);
	if (row==null) return;	
	row.className=row.getAttribute("imsgridstyle");	
	//TODO: add support for multiple selection
	this.lineSelection.value="";
}

DataGrid.prototype.markAll=function (markColumn) {
    if (!Utils.isDefined(this.table)) return;
	var column =this.getColumn(markColumn);
	for(var i=0;i<this.getDataRowCount();i++){
		var control = column.getControl(i);
		if (Utils.isDefined(control))
    		control.setChecked(true);
	}
}

DataGrid.prototype.unmarkAll=function (markColumn) {
    if (!Utils.isDefined(this.table)) return;
	var column = this.getColumn(markColumn);
	for(var i=0;i<this.getDataRowCount();i++){
		var control = column.getControl(i);
		if (Utils.isDefined(control))
    		control.setChecked(false);
	}
}

DataGrid.prototype.markAllEnabled=function (markColumn) {
    if (!Utils.isDefined(this.table)) return;
	var column =this.getColumn(markColumn);
	for(var i=0;i<this.getDataRowCount();i++){
		var control = column.getControl(i);
		if (Utils.isDefined(control) && control.isEnabled())
		{
    		control.setChecked(true);
    	}
	}
}

DataGrid.prototype.findElementRow=function (element){
	var cssClasses=["innertablecellselected","innertablecell","innertablecellalt","innertablecellinvalid"];
	while (element!=null){
		if (element.className&&cssClasses.hasValue(element.className.toLowerCase())){
			return element;
		}	
		element=element.parentNode;
	}
	return null;
}

DataGrid.prototype.getDataCell = function (row, col) {
	return this.table.rows[this.isHeaderVisible?row+1:row].cells[col];
}

DataGrid.prototype.getCell = function (row, col) {
	return this.table.rows[row].cells[col];
}

DataGrid.prototype.finalize = function () {
    this.columns.removeAll();
}
